/**
 * 抽屉管理模块
 */
class DrawerManager {
    constructor() {
        this.isOpen = false;
        this.init();
    }

    init() {
        this.bindEvents();
        
        // 等待layui加载完成
        layui.use(['form'], (form) => {
            this.form = form;
            this.setupForm();
        });
    }

    bindEvents() {
        // 抽屉相关元素
        const drawerBtn = document.getElementById('drawerBtn');
        const customDrawer = document.getElementById('customDrawer');
        const drawerOverlay = document.getElementById('drawerOverlay');
        const drawerClose = document.getElementById('drawerClose');
        
        // 绑定事件
        drawerBtn.addEventListener('click', (e) => {
            e.preventDefault();
            this.openDrawer();
        });
        
        drawerClose.addEventListener('click', () => this.closeDrawer());
        drawerOverlay.addEventListener('click', () => this.closeDrawer());
        
        // ESC键关闭抽屉
        document.addEventListener('keydown', (e) => {
            if (e.key === 'Escape' && this.isOpen) {
                this.closeDrawer();
            }
        });
    }

    setupForm() {
        // 渲染表单
        this.form.render();
        
        // 表单监听事件
        this.form.on('switch', (data) => {
            console.log('开关状态:', data.elem.name, data.elem.checked);
            this.handleSwitchChange(data);
        });
        
        this.form.on('select', (data) => {
            console.log('选择变化:', data.value);
            layui.layer.msg('设置已更新');
        });
    }

    // 打开抽屉
    openDrawer() {
        const customDrawer = document.getElementById('customDrawer');
        const drawerOverlay = document.getElementById('drawerOverlay');
        
        customDrawer.classList.add('open');
        drawerOverlay.classList.add('show');
        document.body.style.overflow = 'hidden';
        this.isOpen = true;
        
        // 更新连接信息
        this.updateConnectionInfo();
        console.log('抽屉已打开');
    }

    // 关闭抽屉
    closeDrawer() {
        const customDrawer = document.getElementById('customDrawer');
        const drawerOverlay = document.getElementById('drawerOverlay');
        
        customDrawer.classList.remove('open');
        drawerOverlay.classList.remove('show');
        document.body.style.overflow = 'auto';
        this.isOpen = false;
        
        console.log('抽屉已关闭');
    }

    // 处理开关变化
    handleSwitchChange(data) {
        const switchName = data.elem.name;
        const checked = data.elem.checked;
        
        switch(switchName) {
            case 'autoExpand':
                layui.layer.msg(checked ? '已开启自动展开' : '已关闭自动展开');
                break;
            case 'showHidden':
                layui.layer.msg(checked ? '显示隐藏文件' : '隐藏隐藏文件');
                // TODO: 实现显示/隐藏文件功能
                break;
            default:
                layui.layer.msg(data.elem.title + '：' + (checked ? '开启' : '关闭'));
        }
    }

    // 更新连接信息
    updateConnectionInfo() {
        // 这个方法可以被TreeManager调用来更新连接状态
        if (window.TreeManager && window.TreeManager.currentConfig) {
            window.TreeManager.updateDrawerConnectionInfo();
        }
    }

    // 获取当前设置
    getCurrentSettings() {
        return {
            autoExpand: document.querySelector('input[name="autoExpand"]')?.checked || false,
            showHidden: document.querySelector('input[name="showHidden"]')?.checked || false,
            maxDepth: document.querySelector('input[name="maxDepth"]')?.value || 3
        };
    }
}

// 创建全局DrawerManager实例
window.DrawerManager = new DrawerManager();